package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;

@Service
public class ThiIndustryService {
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	
	public IndicatorBreakdownDatabean queryCountyRank(
			IndicatorBreakdownFilter filter) {
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists = breakdownService.queryCountyRank(filter);
		if(lists.size()<3){
			return null;
		}
		IndicatorBreakdownDatabean databeans2 = new IndicatorBreakdownDatabean();
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			DecimalFormat df=new DecimalFormat("#.##");
			databean.setRatio(df.format(Double.parseDouble(indicatorBreakdown.getRatio())));
			if("0".equals(indicatorBreakdown.getCumulative())){
				databean.setCumulative("0");
			}else {
				String st=df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000);
				databean.setCumulative(st);
			}
			/*if(indicatorBreakdown.getArea().contains("福州市")){
				databeans.add(0, databean);
			}else if(indicatorBreakdown.getArea().contains("蕉城")){
				String st=df.format(Double.parseDouble(indicatorBreakdown.getExpectedTarget())/10000);
				databeans2.setExpectedTarget(st);
				databeans2.setArea(indicatorBreakdown.getArea());
				databeans.add(2, databean);
			}else if(indicatorBreakdown.getArea().contains("连江县")||indicatorBreakdown.getArea().contains("福清市")
					||indicatorBreakdown.getArea().contains("长乐市")||indicatorBreakdown.getArea().contains("罗源县")
					||indicatorBreakdown.getArea().contains("闽侯县")||indicatorBreakdown.getArea().contains("闽清县")
					||indicatorBreakdown.getArea().contains("马尾区")||indicatorBreakdown.getArea().contains("晋安区")
					||indicatorBreakdown.getArea().contains("仓山区")||indicatorBreakdown.getArea().contains("鼓楼区")
					||indicatorBreakdown.getArea().contains("台江区")){
				databeans.add(databean);
			}*/
			databeans.add(databean);
		}
		databeans2.setRightdata(databeans);
		return databeans2;
	}

	public List<IndicatorBreakdownDatabean> queryDevelopmentSituation(
			IndicatorBreakdownFilter filter) throws ParseException {
		//查找当季和前一个季度数据
		List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists = breakdownService.queryDevelopmentSituation(filter);
		//校验
		if(lists.size()<2){
			return null;
		}
		//TertiaryIndustry industries = industryService.queryByfilter(filter);
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		String lastDateVal = "";
		String DateVal = "";
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -3))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
				databean.setRatio(indicatorBreakdown.getRatio());
			}
			
		}
		System.out.println(lastDateVal);
		System.out.println(DateVal);
		Double double1=0.00;
		if(ObjectUtils.isNotBlank(lastDateVal)){
			 double1 = Double.parseDouble(lastDateVal);
		}else {
			double1=0.00;
		}
		Double double2 = Double.parseDouble(DateVal);
		Double double3 = double2-double1;
		//查找去年当季和去年当季前一个季度数据
		filter.setCurrentMonth(DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(),-12));
		List<IndicatorBreakdown> indicatorBreakdowns = breakdownService.queryDevelopmentSituation(filter);
		for (IndicatorBreakdown indicatorBreakdown : indicatorBreakdowns) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -3))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
			}
		}
		double1 = Double.parseDouble(lastDateVal);
		double2 = Double.parseDouble(DateVal);
		Double double4 = double2-double1;
		double3 =(double3-double4)/double4*100;
		if(double3 != 0){
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(double3);
			databean.setConcurrentGrowth(st);
		}
		//databean.setCountyIncrease(industries.getCountyIncrease());
		//databean.setCityIncrease(industries.getCityIncrease());
		//databean.setuBIncrease(industries.getuBIncrease());
		breakdownDatabean2s.add(databean);
		return breakdownDatabean2s;
	}

	public List<Map<String, Object>> queryAbsoluteValue(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		List<IndicatorBreakdownDatabean> breakdownDatabeans2 = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans3 = new ArrayList<IndicatorBreakdownDatabean>();
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = DataUtils.detailData(breakdownSs,breakdownDatabeans);
		for (IndicatorBreakdownDatabean breakdownDatabean : breakdownDatabeans) {
			String date = breakdownDatabean.getCurrentMonth();
			for (IndicatorBreakdownDatabean databean : breakdownDatabeans) {
				if(databean.getCurrentMonth().contains(DateForIndUtils.getNextMonthDate(date,-12))){
					Double double5 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
					Double double6 = Double.parseDouble(databean.getSeasonsComparison());
					Double double7 = (double5-double6)/double6*100;
					if(double7 != 0){
						DecimalFormat df=new DecimalFormat(".##");
						String st=df.format(double7);
						breakdownDatabean.setConcurrentGrowth(st);
					}
				}
			}
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(Double.parseDouble(breakdownDatabean.getSeasonsComparison())/10000);
			breakdownDatabean.setSeasonsComparison(st);
			if(breakdownDatabean.getCurrentMonth().contains(filter.getDate())){
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans2.add(breakdownDatabean);
			}else {
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans3.add(breakdownDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabeans2);
		Collections.reverse(breakdownDatabeans3);
		map2.put("lastYear", breakdownDatabeans2);
		map2.put("years", breakdownDatabeans3);
		list.add(map2);
		return list;
	}

	public List<Map<String, Object>> queryCumulativeGrowth(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdownSs,breakdownDatabeans);
		List<IndicatorBreakdownDatabean> breakdownDatabean4s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans4 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdownDatabeans) {
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000);
			indicatorBreakdown.setCumulative(st);
			st = df.format(Double.parseDouble(indicatorBreakdown.getRatio()));
			indicatorBreakdown.setRatio(st);
			if(indicatorBreakdown.getCurrentMonth().contains(filter.getDate())){
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabean4s.add(indicatorBreakdown);
			}else {
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabeans4.add(indicatorBreakdown);
			}
		}
		List<Map<String, Object>> list32 = new ArrayList<Map<String,Object>>();
		Map<String, Object> map32 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabean4s);
		Collections.reverse(breakdownDatabeans4);
		map32.put("years",breakdownDatabeans4);
		map32.put("lastYear", breakdownDatabean4s);
		DecimalFormat df=new DecimalFormat("#.##");
		String st=df.format(Double.parseDouble(breakdowns.get(0).getExpectedTarget())/10000);
		map32.put("expectedTarget", st);
		list32.add(map32);
		return list32;
	}

	public List<CompletionClassificationDatabean> queryByZCZB(
			IndicatorBreakdownFilter filter) throws ParseException {
		filter.setIndustryId("A06");
		filter.setIndustryName("第三产业增加值");
		List<CompletionClassification> classifications = classificationService.queryByHYValueSec(filter);
		if(ObjectUtils.isBlank(classifications)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextMonthValueDate(filter.getDate(), -12));
		List<CompletionClassification> classifications2 = classificationService.queryByHYValueSec(filter);
		List<CompletionClassificationDatabean> classificationDatabeans = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassification completionClassification : classifications) {
			for (CompletionClassification completionClassification2 : classifications2) {
				if(completionClassification2.getCategoryDetails().equals(completionClassification.getCategoryDetails())){
					CompletionClassificationDatabean classificationDatabean = new CompletionClassificationDatabean();
					classificationDatabean.setCategoryDetails(completionClassification.getCategoryDetails());
					DecimalFormat df=new DecimalFormat("#.##");
					Double double1 = Double.parseDouble(completionClassification.getActualCompletionValue());
					String st=df.format(double1/1000);
					classificationDatabean.setActualCompletionValue(st);
					Double double2 = Double.parseDouble(completionClassification2.getActualCompletionValue());
					String st2=df.format(double2/1000);
					classificationDatabean.setLastActualCompletionValue(st2);
					Double double3 = (double1-double2)/double2*100;
					st = df.format(double3);
					classificationDatabean.setSpecificIncrease(st);
					st = df.format(Double.parseDouble(completionClassification.getPlanneValue())/1000);
					classificationDatabean.setPlanneValue(st);
					classificationDatabeans.add(classificationDatabean);
				}
			}
		}
		return classificationDatabeans;
	}

	public List<CompletionClassificationDatabean> queryByZCZBSec(
			IndicatorBreakdownFilter filter) {
		filter.setIndustryName("第三产业增加值");
		List<CompletionClassification> classifications = classificationService.queryByHYValueSec(filter);
		if(ObjectUtils.isBlank(classifications)){
			return null;
		}
//		filter.setDate(DateForIndUtils.getNextMonthValueDate(filter.getDate(), -12));
//		List<CompletionClassification> classifications2 = classificationService.queryByHYValueSec(filter);
		List<CompletionClassificationDatabean> classificationDatabeans = new ArrayList<CompletionClassificationDatabean>();
		DecimalFormat df=new DecimalFormat("#.##");
		for (CompletionClassification completionClassification : classifications) {
			CompletionClassificationDatabean classificationDatabean = new CompletionClassificationDatabean();
			classificationDatabean.setActualCompletionValue(df.format(Double.parseDouble(completionClassification.getActualCompletionValue())/1000));
			classificationDatabean.setCategoryDetails(completionClassification.getCategoryDetails());
			classificationDatabean.setCompleteAnnual(df.format(Double.parseDouble(completionClassification.getCompleteAnnual())));
			classificationDatabean.setPlanneValue(df.format(Double.parseDouble(completionClassification.getPlanneValue())/1000));
			classificationDatabeans.add(classificationDatabean);
		}
		return classificationDatabeans;
	}

}
